#-*- coding: utf-8 -*-

from usagi.window import UsagiWindow
from usagi.renshuu import RenshuuWindow
from usagi.paths import *
from usagi.kanji import Kanji
from usagi.tango import Tango
import usagi.sql
import os.path

class UsagiApp:
    def __init__(self):
        if not os.path.exists(SQL_PATH):
            usagi.sql.create_database()
        self.cur = usagi.sql.sql_cur()
        self.tango = Tango(self.cur.fetch_learned())
        self.window=UsagiWindow(self,self.cur)
    
    def master_kanji(self, kanji):
        ntango=self.tango.new_tango(kanji)
        self.cur.add_tango(ntango)
        self.cur.add_kanji(kanji)
        self.window.set_kanji(self.cur.fetch_kanji())
        usagi.window.NewWords(u"新しい漢字",self.window,\
        "The following words have been added to your learning list:","\n".join(ntango)).showanddie()

    def renshuutime(self):
        self.renshuuwin=RenshuuWindow(self,self.cur)
        self.renshuuwin.set_word(self.cur.fetch_word(True))

       
    def back_from_practice(self):
        kanji = self.cur.fetch_kanji(True)
        self.window.set_kanji(kanji)
        self.window.show_all()

    def run(self,k=None):
        kanji = k and k or self.cur.fetch_kanji()
        self.window.main(kanji)

